{{- define "webhook_resources" }}
cpu: 10m
memory: 25Mi
{{- end }}

{{- if (.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: webhook
  namespace: d8-cert-manager
  {{- include "helm_lib_module_labels" (list . (dict "app" "webhook" "workload-resource-policy.deckhouse.io" "master")) | nindent 2 }}
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: webhook
  updatePolicy:
    updateMode: "Initial"
  resourcePolicy:
    containerPolicies:
    - containerName: "webhook"
      minAllowed:
        {{- include "webhook_resources" . | nindent 8 }}
      maxAllowed:
        cpu: 20m
        memory: 50Mi
{{- end }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: webhook
  namespace: d8-cert-manager
  {{- include "helm_lib_module_labels" (list . (dict "app" "webhook")) | nindent 2 }}
spec:
  {{- include "helm_lib_deployment_on_master_strategy_and_replicas_for_ha" . | nindent 2 }}
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: webhook
  template:
    metadata:
      annotations:
        checksum/extension-apiserver-authentication: {{ .Values.global.discovery.extensionAPIServerAuthenticationRequestheaderClientCA | sha256sum }}
        checksum/certificate: {{ include (print .Template.BasePath "/webhook/secret-tls.yaml") . | sha256sum }}
      labels:
        app: webhook
    spec:
      serviceAccountName: webhook
      {{- include "helm_lib_priority_class" (tuple . "system-cluster-critical") | nindent 6 }}
      {{- include "helm_lib_pod_anti_affinity_for_ha" (list . (dict "app" "webhook")) | nindent 6 }}
      {{- include "helm_lib_node_selector" (tuple . "master") | nindent 6 }}
      {{- include "helm_lib_tolerations" (tuple . "any-node" "with-uninitialized") | nindent 6 }}
      {{- include "helm_lib_module_pod_security_context_run_as_user_nobody" . | nindent 6 }}
      imagePullSecrets:
      - name: deckhouse-registry
      containers:
      - name: webhook
        {{- include "helm_lib_module_container_security_context_read_only_root_filesystem" . | nindent 8 }}
        image: {{ include "helm_lib_module_image" (list . "certManagerWebhook") }}
        args:
        - --v=3
        - --secure-port=6443
        - --tls-cert-file=/certs/tls.crt
        - --tls-private-key-file=/certs/tls.key
        env:
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        livenessProbe:
          httpGet:
            path: /livez
            port: 6080
            scheme: HTTP
        readinessProbe:
          httpGet:
            path: /healthz
            port: 6080
            scheme: HTTP
        volumeMounts:
        - name: certs
          mountPath: /certs
        - mountPath: /tmp
          name: tmp
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_logs_with_extra" 10 | nindent 12 }}
{{- if not (.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "webhook_resources" . | nindent 12 }}
{{- end }}
      volumes:
      - name: certs
        secret:
          secretName: cert-manager-webhook-tls
      - name: tmp
        emptyDir: {}
